const mysql = require('mysql2/promise');
const config = require('./database-config.js');

async function clearTrainingStatus() {
  const connection = await mysql.createConnection(config.source);
  
  console.log('🧹 清理角色训练状态...');
  
  try {
    // 查询当前训练状态
    const [trainings] = await connection.execute('SELECT * FROM character_training WHERE is_training = 1');
    console.log(`📋 找到 ${trainings.length} 个正在进行的训练`);
    
    if (trainings.length > 0) {
      trainings.forEach(training => {
        console.log(`  - 角色ID: ${training.character_id}, 训练小时: ${training.training_hours}`);
      });
      
      // 清理所有正在进行的训练
      await connection.execute('UPDATE character_training SET is_training = 0, training_status = 0, training_start_time = NULL, training_end_time = NULL, training_hours = 0 WHERE is_training = 1');
      console.log('✅ 已清理所有正在进行的训练状态');
    } else {
      console.log('✅ 没有正在进行的训练');
    }
    
    // 查询角色信息
    const [characters] = await connection.execute('SELECT id, name, lvl, exp FROM characters WHERE id IN (2001, 2007)');
    console.log('📊 角色信息:');
    characters.forEach(char => {
      console.log(`  - ${char.name} (ID: ${char.id}): 等级=${char.lvl}, 经验=${char.exp}`);
    });
    
  } catch (error) {
    console.error('❌ 清理失败:', error.message);
  } finally {
    await connection.end();
  }
}

clearTrainingStatus();
